package com.rp.user.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.rp.bo.UpdateUserInfoBO;
import com.rp.news.common.RedisCommon;
import com.rp.news.common.ResponseStatusEnum;
import com.rp.news.common.Result;
import com.rp.news.util.RedisOperator;
import com.rp.pojo.AppUser;
import com.rp.user.service.UserService;
import com.rp.user.service.impl.MyFanServiceImpl;
import com.rp.vo.AppUserVo;
import com.rp.vo.UserAccountInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
/**
 * 和配置文件配合进行全局服务降级
 */
//@DefaultProperties(defaultFallback = "defaultFallback")

@Api(value = "user相关接口文档", tags = {"用户congtroller api文档"})
@RequestMapping("/user")

public class UserController {


    //public Result defaultFallback() {
//        return Result.errorMsg("系统默认全局降级");
//    }

    @Autowired
    private UserService userService;


    @Autowired
    public RedisOperator redis;

    @Autowired
    public RestTemplate restTemplate;


    @ApiOperation(value = "获取用户基本信息", notes = "获取用户基本信息", httpMethod = "POST")
    @PostMapping("/getUserInfo")
    public Result getUserInfo(String userId) {
        if (StrUtil.isBlank(userId)) {
            return Result.errorCustom(ResponseStatusEnum.UN_LOGIN);
        }

        AppUser user = getUser(userId);

        AppUserVo convert = Convert.convert(AppUserVo.class, user);

        //查询redis 中的用户 得到用户关注数和粉丝数  放入 appuservo
        convert.setMyFansCounts(redis.getCountsFromRedis(MyFanServiceImpl.REDIS_MY_FOLLOW_COUNTS + convert.getId()));
        convert.setMyFollowCounts(redis.getCountsFromRedis(MyFanServiceImpl.REDIS_MY_FOLLOW_COUNTS + convert.getId()));
        return Result.ok(convert);
    }

    @ApiOperation(value = "获取用户账号信息", notes = "获取用户账号信息", httpMethod = "POST")
    @PostMapping("/getAccountInfo")
    public Result getAccountInfo(String userId) {

        if (StrUtil.isBlank(userId)) {
            return Result.errorCustom(ResponseStatusEnum.UN_LOGIN);
        }

        AppUser user = getUser(userId);

        UserAccountInfoVo convert = Convert.convert(UserAccountInfoVo.class, user);

        return Result.ok(convert);
    }

    @ApiOperation(value = "修改完善用户信息", notes = "修改完善用户信息", httpMethod = "POST")
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@Valid UpdateUserInfoBO updateUserInfoBO) {
        userService.updateUserInfo(updateUserInfoBO);
        return Result.ok();
    }

    //配置全局服务降级 应该注释掉(fallbackMethod = "queryByIdsFallback")
    //@HystrixCommand //(fallbackMethod = "queryByIdsFallback")
    @ApiOperation(value = "根据用户ids查询用户列表", notes = "根据用户ids查询用户列表", httpMethod = "GET")
    @GetMapping("/queryByIds")
    public Result queryByIds(String userIds) {
        int i = 1 / 0;

//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        if (StrUtil.isBlank(userIds)) {
            return Result.ok(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
        }

        List<AppUserVo> result = new ArrayList<>();
        String[] split = userIds.split(",");
        for (String userId : split) {
            //查询用户信息
            AppUser user = getUser(userId);

            //返回用户信息
            AppUserVo convert = Convert.convert(AppUserVo.class, user);

            result.add(convert);
        }


        return Result.ok(result);
    }

    private Result queryByIdsFallback(String userIds) {

        //返回用户信息
        AppUserVo convert = new AppUserVo();

        return Result.ok(convert);
    }

    private AppUser getUser(String userId) {

        String userStr = redis.get(RedisCommon.REDIS_USER_INFO + ":" + userId);
        if (StrUtil.isNotBlank(userStr)) {
            AppUser appUser = JSONUtil.toBean(userStr, AppUser.class);
            return appUser;
        }

        AppUser user = userService.getUser(userId);

        redis.set(RedisCommon.REDIS_USER_INFO + ":" + userId, JSONUtil.toJsonStr(user));
        return user;
    }


}
